/**
 * 异步加载列表数据
 */
function list(pageNo,pid)
{
	var url = getAction()+"?cmd=data&"+getForm().formSerialize();
	
	$.ajax({
		type: "POST",
		url: url,
		success: function(returnedData){
			$("#dataList").get(0).innerHTML=returnedData;
		}
	});
}

function getAction(){
	return "dataDictionary.action";
}

function getForm(){
	return $("#dataDictionaryForm");
}

function getDomForm(){
	return $$("dataDictionaryForm");
}
/*
 * 跳转到添加页面
 */
function goAdd(){
	var retValue = window.showModalDialog(getAction()+"?cmd=goAdd","menubar=no;toolbar=no");
}

/*
 * 添加操作，然后返回list页面
 */
function add(){
	if(!validateForm()) return;
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url);
}


/**
 * 添加后返回添加页面继续添加一个记录
 */
function addNext()
{
	if(!validateForm())	return;
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url,"addNext");

}

/**
 * 更新一条记录
 */
function update()
{
	if(!validateForm()) return;
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url);
}



function saveOrUpdate(url,type){
	$.ajax({
		type: "POST",
		url: url,
		dataType:"html",
		success: function(returnedData){
			returnedData = $.evalJSON(returnedData);
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				var parentWindow = window.dialogArguments;
				parentWindow.list(1);
			}
			if(type !== 'addNext' )	//未定义
				window.close();
			getForm().get(0).reset();		//重置
		}
	});
	
}

/**
 * 跳转到更新页面
 */
function goUpdate(id)
{
	var retValue = window.showModalDialog(getAction()+"?cmd=goUpdate&po.id="+id,window,"menubar=no;toolbar=no");
	if(retValue=='1'){
		list($("pageForm.pageNo").value);
	}
}


/**
 * 表单域校验
 */ 
function validateForm()
{
	var success = true;
	if(!checkBlank($$("po.key"),"字典类型")||!checkCharNumber($$("po.key"),"字典类型")||!MaxLength($$("po.key"),"字典类型",20))
	{
		success = false;
	}
	else if(!checkBlank($$("po.text"),"显示内容")||!MaxLength($$("po.text"),"显示内容",50))
	{
		success = false;
	}
	else if(!checkBlank($$("po.value"),"value")||!MaxLength($$("po.value"),"value",20))
	{
		success = false;
	}
	return success;
}

function change_option(number, index){
 	for (var i = 1; i <= number; i++) {
      document.getElementById('current' + i).className = '';
      document.getElementById('content' + i).style.display = 'none';
 	}
    document.getElementById('current' + index).className = 'current';
    document.getElementById('content' + index).style.display = 'block';
}

function initMultiChoice(){
	var flag = $$("flag").value
	var number = 7;
	var index = 1;
	var url = getAction()+"?cmd=view&formMap.flag="+flag;
	if(flag == "platform"){
		index=1;
	}else if(flag == "company"){
		index=2;
	}else if(flag == "department"){
		index=3;
	}else if(flag == "identifyOrg"){
		index=4;
	}else if(flag == "majorType"){
		index=5;
	}else if(flag == "levelName"){
		index=6;
	}else if(flag == "reviewPeriod"){
		index=7;
		if($$("sourceId").value.trim() != ''){
			url = url +"&formMap.sourceId="+$$("sourceId").value.trim();
		}
	}else {
		alert("所选的tab无效");
		return;
	}
	$.ajax({
		type: "POST",
		url: url,
		dataType:"html",
		success: function(returnedData){
			$("#contentMain"+index).get(0).innerHTML=returnedData;
			change_option(number,index);
			if(index != 7){
				var str = window.dialogArguments.document.getElementById($$("targetId").value).value.trim();
				if(str != ''){
					var astr = str.split(",");
					for(var i=0;i<astr.length;i++){
						$("#optional").find("option").each(function(){
							if($(this).text().trim()==astr[i])
							$("#selectedData").append($(this));
						});
					}
				}
			}
		}
	});
}

function addItem(){
	var flag = $$("flag").value;
	if(flag !='company' && flag !='department' && flag !='platform'){
		if($("#selectedData option").size()>=1){
			alert("此项为单选,您已选择");
			return;
		}
		if($("#optional").find("option:selected").size()>1){
			alert("只能选一个");
			return;
		}
	}
	$("#optional").find("option:selected").each(function(){
		$("#selectedData").append($(this));
	});
	var ext = $("#external").val();
	if(ext!=null && ext.trim()!=''){
		$("#selectedData").append($('<option value="">'+ext.trim()+"</option>"));
		ext = encodeURIComponent(ext.trim());
		$("#external").val('');
		var url = getAction()+"?cmd=add&po.text="+ext+"&po.value="+ext+"&po.key="+flag+"&po.description=extra";
		$.ajax({
			type: "POST",
			url: url,
			dataType:"html",
			success: function(returnedData){
			}
		});
	}
}

function addItemed(){
	var ext = $("#external").val();
	var flag = $$("flag").value;
	if(ext!=null && ext.trim()!=''){
		$("#optional").append($('<option value="">'+ext.trim()+"</option>"));
		$("#external").val('');
		ext = encodeURIComponent(ext.trim());
		var url = getAction()+"?cmd=add&po.text="+ext+"&po.value="+ext+"&po.key="+flag+"&po.description=extra";
		$.ajax({
			type: "GET",
			url: url,
			dataType:"html",
			success: function(returnedData){
			}
		});
	}
}

function removeItem(){
	$("#selectedData").find("option:selected").each(function(){
		if($(this).val().trim()==''){
			 $("#external").val($(this).text().trim());
			 $(this).remove();
		}else{
			$("#optional").append($(this));
		}
	});
}

function addMultiChoice(){
	var str = "";
	var sentDep = "";
	var sentDepName = "";
	$("#selectedData option").each(function(){
		if(str==""){
			str = $(this).text().trim();
		}else{
			str = str + ","+$(this).text().trim();
		}
		var sd = $(this).val().trim();
		var sdName = $(this).text().trim();
		if(sentDep==""){
			if(sd!=''  && sd.indexOf("extra")==-1){
				sentDep = sd.substring(0,sd.length-1);
				sentDepName = sdName;
			}
		}else{
			if(sd!='' && sd.indexOf("extra")==-1){
				sentDep = sentDep + "," +sd.substring(0,sd.length-1);
				sentDepName = sentDepName+","+sdName;
			}
		}
	});
	var flag = $$("flag").value;
	var isSentDep = $$("isSentDep").value;
	var id = "";
	if(flag=='company' && isSentDep=='YES'){
		window.dialogArguments.document.getElementById("po.sentDep").value = sentDep;
		window.dialogArguments.document.getElementById("po.sentDepName").value = sentDepName;
	}
	window.dialogArguments.document.getElementById($$("targetId").value).value = str;
	window.close();
}

function addReviewPeriod(){
	var editmaterial = $$('po.editmaterial');
	var unitaudit = $$('po.unitaudit');
	var companyaudit = $$('po.companyaudit');
	var seal = $$('po.seal');
	var submission = $$('po.submission');
	var publicity = $$('po.publicity');
	var officialdocument = $$('po.officialdocument');
	var commend = $$('po.commend');
	var parentWindow = window.dialogArguments.document;

	if(commend.value.trim() != ''){
		if(validateReviewPeriod('commend',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = commend.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return ;
		}
	}else if(officialdocument.value.trim() != ''){
		if(validateReviewPeriod('officialdocument',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = officialdocument.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return ;
		}
	}else if(publicity.value.trim() != ''){
		if(validateReviewPeriod('publicity',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = publicity.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return ;
		}
	} else if(submission.value.trim() != ''){
		if(validateReviewPeriod('submission',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = submission.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		} else{
			return;
		}
	} else if(seal.value.trim() != ''){
		if(validateReviewPeriod('seal',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = seal.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return;
		}
	}else if(companyaudit.value.trim() != ''){
		if(validateReviewPeriod('companyaudit',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = companyaudit.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return;
		}
	} else if(unitaudit.value.trim() != ''){
		if(validateReviewPeriod('unitaudit',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = unitaudit.name;
			setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return;
		}
	} else {
		if(validateReviewPeriod('editmaterial',editmaterial,unitaudit,companyaudit,seal,submission,publicity,officialdocument)){
			parentWindow.getElementById($$("targetId").value).value = editmaterial.name;
	 		setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
				,seal,submission,publicity,officialdocument,commend);
		}else{
			return;
		}
	}
	window.close();
}

function setReviewPeriodValue(parentWindow,editmaterial,unitaudit,companyaudit
		,seal,submission,publicity,officialdocument,commend){
		parentWindow.getElementById("po.editmaterial").value = editmaterial.value.trim();
		parentWindow.getElementById("po.unitaudit").value = unitaudit.value.trim();
		parentWindow.getElementById("po.companyaudit").value = companyaudit.value.trim();
		parentWindow.getElementById("po.seal").value = seal.value.trim();
		parentWindow.getElementById("po.submission").value = submission.value.trim();
		parentWindow.getElementById("po.publicity").value = publicity.value.trim();
		parentWindow.getElementById("po.officialdocument").value = officialdocument.value.trim();
		parentWindow.getElementById("po.commend").value = commend.value.trim();
}

function validateReviewPeriod(flag,obj1,obj2,obj3,obj4,obj5,obj6,obj7){
	var ret = true;
	if(flag=='commend'){
		if(obj1.value.trim()==''){
			alert(obj1.name+"不能为空!");
			ret = false;
		}else if (obj2.value.trim()==''){
			alert(obj2.name+"不能为空!");
			ret =false;
		}else if (obj3.value.trim()==''){
			alert(obj3.name+"不能为空!");
			ret = false;
		}else if (obj4.value.trim()==''){
			alert(obj4.name+"不能为空!");
			ret = false;
		}else if (obj5.value.trim()==''){
			alert(obj5.name+"不能为空!");
			ret = false;
		}else if (obj6.value.trim()==''){
			alert(obj6.name+"不能为空!");
			ret = false;
		}else if (obj7.value.trim()==''){
			alert(obj7.name+"不能为空!");
			ret = false;
		}
	}
	else if(flag=='officialdocument'){
		if(obj1.value.trim()=='') {
			alert(obj1.name+"不能为空!");
			ret = false;
		}else if (obj2.value.trim()==''){
			alert(obj2.name+"不能为空!");
			ret = false;
		}else if (obj3.value.trim()==''){
			alert(obj3.name+"不能为空!");
			ret = false;
		}else if (obj4.value.trim()==''){
			alert(obj4.name+"不能为空!");
			ret = false;
		}else if (obj5.value.trim()==''){
			alert(obj5.name+"不能为空!");
			ret = false;
		}else if (obj6.value.trim()==''){
			alert(obj6.name+"不能为空!");
			ret = false;
		}
	}
	else if(flag=='publicity'){
		if(obj1.value.trim()==''){
			alert(obj1.name+"不能为空!");
			ret = false;
		}else if (obj2.value.trim()==''){
			alert(obj2.name+"不能为空!");
			ret = false;
		}else if (obj3.value.trim()==''){
			alert(obj3.name+"不能为空!");
			ret = false;
		}else if (obj4.value.trim()==''){
			alert(obj4.name+"不能为空!");
			ret = false;
		}else if (obj5.value.trim()==''){
			alert(obj5.name+"不能为空!");
			ret = false;
		}
	}
	else if(flag=='submission'){
		if(obj1.value.trim()==''){
			ret = false;
			alert(obj1.name+"不能为空!");
		}else if (obj2.value.trim()==''){
			ret = false;
			alert(obj2.name+"不能为空!");
		}else if (obj3.value.trim()==''){
			alert(obj3.name+"不能为空!");
			ret = false;
		}else if (obj4.value.trim()==''){
			alert(obj4.name+"不能为空!");
			ret = false;
		}
	}
	else if(flag=='seal'){
		if(obj1.value.trim()==''){
			ret = false;
			alert(obj1.name+"不能为空!");
		}else if (obj2.value.trim()==''){
			alert(obj2.name+"不能为空!");
			ret = false;
		}else if (obj3.value.trim()==''){
			ret = false;
			alert(obj3.name+"不能为空!");
		}
	}
	else if(flag=='companyaudit'){
		if(obj1.value.trim()==''){
			ret = false;
			alert(obj1.name+"不能为空!");
		}else if (obj2.value.trim()==''){
			ret = false;
			alert(obj2.name+"不能为空!");
		}
	}
	else if(flag=='unitaudit'){
		if(obj1.value.trim()==''){
			alert(obj1.name+"不能为空!");
			ret = false;
		}
	}else if(flag=='editmaterial'){
		if(obj1.value.trim()==''){
			alert("请填写时间");
			ret = false;
		}
	}
	return ret;
}
